<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of exception types with their corresponding custom log levels.
     *
     * @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
     */
    protected $levels = [
        //
    ];

    /**
     * A list of the exception types that are not reported.
     *
     * @var array<int, class-string<\Throwable>>
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed to the session on validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        /*$this->reportable(function (Throwable $e) {
            //
        });*/

        $this->reportable(function (ErrorException $e){
            Log::channel('custom')->error($e->getMessage());
        })->stop();

        $this->renderable(function (Throwable $e, $request){
            if($request->ajax()){
                return response()->json(['code'=>$e->getCode(), 'msg'=>$e->getMessage()]);
            }else{
                return response()->view('errors.custom', ['msg'=>$e->getMessage()], 500);
            }
        });
    }

    public function report(Throwable $exception)
    {
        parent::report($exception);
        // 在这里添加自定义的报告逻辑，例如发送邮件给开发者等。
    }

    public function render($request, Throwable $exception)
    {
        return parent::render($request, $exception);


  /*      try {
            // 可能会抛出异常的代码
        } catch (\Exception $e) {
            // 处理异常，例如记录日志或返回特定的响应。
            \Log::error($e->getMessage());
        }*/

    }

}
